<?php

/**
 * This is the model class for table "projects".
 *
 * The followings are the available columns in table 'projects':
 * @property string $id
 * @property string $name
 * @property integer $opened
 * @property string $user_id
 * @property string $created
 */
class Projects extends JLActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Projects the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'projects';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			// array('id', 'required'),
			array('opened', 'numerical', 'integerOnly'=>true),
			array('id, user_id', 'length', 'max'=>16),
			array('name, created', 'length', 'max'=>200),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, opened, user_id, created', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'author'=>array(self::BELONGS_TO, 'Users', 'users_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'opened' => 'Opened',
			'user_id' => 'User',
			'created' => 'Created',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('opened',$this->opened);
		$criteria->compare('user_id',$this->user_id,true);
		$criteria->compare('created',$this->created,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function checkProject(){
		if(Yii::app()->user->isGuest) return false;
		$model = $this->findByAttributes(array(
			'user_id'=>currentUser(true)->id
		));
		if(!empty($model)) return true;
		else return false;
	}
	public function getProject(){
		if(Yii::app()->user->isGuest) return array();
		return $this->findByAttributes(array(
			'user_id'=>currentUser(true)->id
		));
	}
	public function create(){
	
	
		if(!empty($_POST)){
			if(Yii::app()->user->isGuest) return false;
			if($this->checkProject()) return false;
			
			try{
				$model = new Projects();
				$model->user_id = currentUser(true)->id;
				$model->name = _t("Projects 1");
				$model->created = time();
				
				if($model->save()) return true;
				else return false;
			}catch(Exception $e){
				return false;
			}
		
		}else{
			return false;
		}
	
	
	}
}